﻿using DFGK.WMS.Common.Result;
using DFGK_WMS.IRepository.IService;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DFGK_WMS.Repository.Service
{
    public class MaterialService : BaseService, IMaterialService
    {
        public MaterialService(ISqlSugarClient client) : base(client)
        {

        }


        public ApiResult<PagingData<TMaterial>> GetMaterialList(int pageIndex, int pageSize, string? code, string? name, int? materTypeId)
        {
            var materialQuable =
                _Client.Queryable<TMaterial>().AS("TMaterials")
                .WhereIF(!string.IsNullOrEmpty(code), it => it.Code.Contains(code))
                .WhereIF(!string.IsNullOrEmpty(name), it => it.Name.Contains(name))
                .WhereIF(materTypeId != null, it => it.MaterialCategoryId == materTypeId)
                .OrderBy(it => it.Id);

            int totalCount = 0;

            return new ApiResult<PagingData<TMaterial>>()
            {
                Data = new PagingData<TMaterial>()
                {
                    DataList = materialQuable.ToPageList(pageIndex, pageSize, ref totalCount),
                    RecordCount = totalCount
                },
                Code = "200",
                Success = true

            };



        }
        /// <summary>
        /// 获取菜单递归列表
        /// </summary>
        /// <param name="materialName"></param>
        /// <returns></returns>
        public List<MaterialCategoryDto> GetMaterialType(string? materialName)
        {
            Expression<Func<TMaterialCategory, bool>> funWhere = null;

            if (!string.IsNullOrEmpty(materialName))
            {
                funWhere = Expressionable
                        .Create<TMaterialCategory>()
                        .And(t => t.Name.Equals(materialName) && t.IsLeaf == true)
                        .Or(t => t.IsLeaf == false)
                        .ToExpression();
            }
            var materialList = _Client.Queryable<TMaterialCategory>().AS("TMaterialCategories")
                .WhereIF(funWhere != null, funWhere)
                .OrderBy(x => x.Id)
            .ToList();

            //顶级菜单
            materialList.Add(new TMaterialCategory()
            {
                Id = 0,
                Name = "顶级分类",
                Code = "MaterialMenu",
                ParentId = -1
            });

            int minId = materialList.OrderBy(it => it.ParentId).FirstOrDefault()?.ParentId ?? -1;

            var result = GetMaterialByCode(materialList, minId);


            return result;

        }

        private List<MaterialCategoryDto> GetMaterialByCode(List<TMaterialCategory> currentMaterial, int nextId = -1)
        {
            return currentMaterial.
             Where(t => t.ParentId == nextId).Select(it => new MaterialCategoryDto
             {
                 Id = it.Id,
                 Name = it.Name,
                 Code = it.Code,
                 ParentId = it.ParentId,
                 Children = GetMaterialByCode(currentMaterial, it.Id)
             }).
             OrderBy(x => x.Id).ToList();


        }


    }
}
